﻿using Cheems.Control.Common;
using Cheems.Interface.Models.UserManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Cheems.Control.Controls
{
    public class UserCard : UserControl
    {

        /// <summary>
        /// 用户信息
        /// </summary>
        public T_User User
        {
            get { return (T_User)GetValue(UserProperty); }
            set { SetValue(UserProperty, value); }
        }
        public static readonly DependencyProperty UserProperty =
            DependencyProperty.Register("User", typeof(T_User), typeof(UserCard), new PropertyMetadata(new T_User() { CreatTime = DateTime.MinValue}, OnUserChanged));

        /// <summary>
        /// 用户头像
        /// </summary>
        public ImageSource ProfilePicture
        {
            get { return (ImageSource)GetValue(ProfilePictureProperty); }
            set { SetValue(ProfilePictureProperty, value); }
        }
        public static readonly DependencyProperty ProfilePictureProperty =
            DependencyProperty.Register("ProfilePicture", typeof(ImageSource), typeof(UserCard), new PropertyMetadata(null));


        /// <summary>
        /// 当前是否有用户登录
        /// </summary>
        public bool IsUserLoggedIn
        {
            get { return (bool)GetValue(IsUserLoggedInProperty); }
            set { SetValue(IsUserLoggedInProperty, value); }
        }
        public static readonly DependencyProperty IsUserLoggedInProperty =
            DependencyProperty.Register("IsUserLoggedIn", typeof(bool), typeof(UserCard), new PropertyMetadata(false));



        /// <summary>
        /// 用户信息发生改变
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void OnUserChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UserCard thisControl = d as UserCard;
            if (thisControl==null || thisControl.User == null || string.IsNullOrEmpty(thisControl.User.Name))
            {
                thisControl.ProfilePicture = null;
                thisControl.IsUserLoggedIn = false;
                return;
            }
            else if (!string.IsNullOrEmpty(thisControl.User.Name))
            {
                thisControl.IsUserLoggedIn = true;
            }
            // 更新头像
            thisControl.ProfilePicture = IdenticonGenerator.Generate(thisControl.Name);
        }

        public static readonly DependencyProperty UserLoggedInCommandProperty = DependencyProperty.Register(
            "UserLoggedInCommand", typeof(ICommand), typeof(UserCard), new PropertyMetadata(null));
        /// <summary>
        /// 登录事件
        /// </summary>
        public ICommand UserLoggedInCommand
        {
            get { return (ICommand)GetValue(UserLoggedInCommandProperty); }
            set { SetValue(UserLoggedInCommandProperty, value); }
        }

        public static readonly DependencyProperty UserSwitchedCommandProperty = DependencyProperty.Register(
            "UserSwitchedCommand", typeof(ICommand), typeof(UserCard), new PropertyMetadata(null));
        /// <summary>
        /// 切换用户
        /// </summary>
        public ICommand UserSwitchedCommand
        {
            get { return (ICommand)GetValue(UserSwitchedCommandProperty); }
            set { SetValue(UserSwitchedCommandProperty, value); }
        }

        public static readonly DependencyProperty UserLoggedOutCommandProperty = DependencyProperty.Register(
            "UserLoggedOutCommand", typeof(ICommand), typeof(UserCard), new PropertyMetadata(null));
        /// <summary>
        /// 退出登录
        /// </summary>
        public ICommand UserLoggedOutCommand
        {
            get { return (ICommand)GetValue(UserLoggedOutCommandProperty); }
            set { SetValue(UserLoggedOutCommandProperty, value); }
        }
    }
}
